﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LinWon.Core.DAL
{
    public class DALMember
    {
        private LinWon.Core.Data.SqlHelper helper = LinWon.Core.Data.DataFactory.GetHelper();
        /// <summary>
        /// 查询会员
        /// </summary>
        /// <param name="srName">要搜索的会员名称</param>
        /// <param name="srSt">会员的状态</param>
        /// <param name="sortExpression">排序表达式</param>
        /// <param name="startRowIndex">开始行索引</param>
        /// <param name="maximumRows"></param>
        /// <returns></returns>
        public IList<Model.Member> GetMember(string srName, Model.MemberState srSt, string sortExpression, int startRowIndex, int maximumRows)
        {
            StringBuilder strSql = new StringBuilder("SELECT AutoID, RegionID, [Name], CreateTime, EndTime, [State], IsAdmin, MaxUser, WebSite,Email, Tel, Fax, [Address], ZipCode, [Description], LogoURL FROM vMemberInfo");

            StringBuilder strWhere = new StringBuilder();

            List<System.Data.SqlClient.SqlParameter> parms = new List<System.Data.SqlClient.SqlParameter>();

            if (!string.IsNullOrEmpty(srName))
            {
                strWhere.Append(" AND [Name] like @Name");
                System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter("@Name", System.Data.SqlDbType.VarChar, 50);
                p.Value = srName;
                parms.Add(p);
            }
            if (srSt != LinWon.Core.Model.MemberState.All)
            {
                strWhere.Append(" AND [State]=@State");
                System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter("@State", System.Data.SqlDbType.Int, 4);
                p.Value = (int)srSt;
                parms.Add(p);
            }

            if (strWhere.Length > 0)
            {
                strSql.Append(" WHERE");
                strWhere.Remove(0, 4);
                strSql.Append(strWhere.ToString());
            }

            if (!string.IsNullOrEmpty(sortExpression))
                strSql.Append(" "+sortExpression);

            System.Data.SqlClient.SqlDataReader dr = null;

            try
            {
                string sql = PageHelper.GetPageSqlBySqlSever("(" + strSql.ToString() + ")", startRowIndex, maximumRows);
                dr = helper.ExecuteReader(sql, parms.ToArray());
                List<Model.Member> result = new List<LinWon.Core.Model.Member>();

                while (dr.Read())
                {
                    Model.Member m = new LinWon.Core.Model.Member();
                    m.FillByDataReader(dr);
                    result.Add(m);
                }

                return result;
            }
            finally
            {
                if (null != dr && !dr.IsClosed)
                    dr.Close();
            }
        }

        /// <summary>
        /// 获取会员数
        /// </summary>
        /// <param name="srName">要搜索的会员名称</param>
        /// <param name="srSt">会员的状态</param>
        /// <returns></returns>
        public int GetCount(string srName, Model.MemberState srSt)
        {
            StringBuilder strSql = new StringBuilder("SELECT COUNT(1) FROM vMemberInfo");

            StringBuilder strWhere = new StringBuilder();

            List<System.Data.SqlClient.SqlParameter> parms = new List<System.Data.SqlClient.SqlParameter>();

            if (!string.IsNullOrEmpty(srName))
            {
                strWhere.Append(" AND [Name] like @Name");
                System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter("@Name", System.Data.SqlDbType.VarChar, 50);
                p.Value = srName;
                parms.Add(p);
            }
            if (srSt != LinWon.Core.Model.MemberState.All)
            {
                strWhere.Append(" AND [State]=@State");
                System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter("@State", System.Data.SqlDbType.Int, 4);
                p.Value = (int)srSt;
                parms.Add(p);
            }

            if (strWhere.Length > 0)
            {
                strSql.Append(" WHERE");
                strWhere.Remove(0, 4);
                strSql.Append(strWhere.ToString());
            }

            object obj = helper.ExecuteScalar(strSql.ToString(), parms.ToArray());
            int result = 0;

            int.TryParse(obj.ToString(), out result);

            return result;
        }
    }
}
